package com.baibei.order.bill;

import android.os.Bundle;
import android.support.design.widget.RaeTabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.order.bill.deliveryquery.DeliveryQueryFragment;
import com.baibei.order.bill.orderrecord.OrderRecordFragment;
import com.baibei.order.bill.querymoney.PaymentPickUpRecordFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = AppRouter.ROUTER_BILL)
public class MyBillActivity extends BasicActivity {

    @BindView(R2.id.tablayout)
    RaeTabLayout mTablayout;
    @BindView(R2.id.view_pager)
    ViewPager mViewPager;

    /**
     * 标题
     */
    private String[] mTitles;

    /**
     * 订货记录
     * 收支记录
     * 取货记录
     */
    OrderRecordFragment mOrderRecordFragment;
    PaymentPickUpRecordFragment mPickUpRecordFragment;
    DeliveryQueryFragment mDeliveryQueryFragment;
    private List<Fragment> mFragmentList;
    FragmentAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bill);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mTitles = new String[]{getString(R.string.order_record), getString(R.string.revenue_and_expenditure_records), getString(R.string.payment_pick_up_record)};
        for (String title : mTitles) {
            mTablayout.addTab(mTablayout.newTab().setText(title));
        }
        mFragmentList = new ArrayList<>();
        mOrderRecordFragment = OrderRecordFragment.newInstance();
        mPickUpRecordFragment = PaymentPickUpRecordFragment.newInstance();
        mDeliveryQueryFragment = DeliveryQueryFragment.newInstance();
        mFragmentList.add(mOrderRecordFragment);
        mFragmentList.add(mPickUpRecordFragment);
        mFragmentList.add(mDeliveryQueryFragment);

        mAdapter = new FragmentAdapter(getSupportFragmentManager());
        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setAdapter(mAdapter);
        mTablayout.setupWithViewPager(mViewPager);
    }

    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }

    private class FragmentAdapter extends FragmentPagerAdapter {

        FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }
}
